package com.atguigu.gmall.weball.controller;

import com.atguigu.gmall.cart.vo.SuccessCartVo;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.cart.CartFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @Author:lyd
 * @Date:2023/9/1 16:56
 * @Version:1.0
 * @Description
 **/
@Controller
public class CartController {

    @Autowired
    CartFeignClient cartFeignClient;

    /**
     * 向购物车中添加商品，跳转到添加成功页面
     *
     * @param skuId
     * @param skuNum
     * @param model
     * @return
     */
    //http://cart.gmall.com/addCart.html?skuId=49&skuNum=1&sourceType=query
    @GetMapping("/addCart.html")
    public String addCart(@RequestParam("skuId") Long skuId,
                          @RequestParam("skuNum") Integer skuNum,
                          Model model) {
        Result<SuccessCartVo> result = cartFeignClient.add(skuId, skuNum);

        if (result.getCode() == 200) {
            model.addAttribute("skuInfo", result.getData().getSkuInfo());
            model.addAttribute("skuNum", result.getData().getSkuNum());
        }else {
            model.addAttribute("msg", result.getMessage());
        }

        return "cart/addCart";
    }

    /**
     * 展示商品添加成功的列表
     *
     * @return
     */
    @GetMapping("/cart.html")
    public String cart() {
        return "cart/index";
    }

    /**
     * 批量删除购物车中的商品数据
     * @return
     */
    @GetMapping("/cart/deleteChecked")
    public String deleteChecked() {
        cartFeignClient.deleteChecked();
        return "redirect:http://cart.gmall.com/cart.html";
    }

}
